﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FRW.Common.Forms
{
    public partial class ProgressForm : Form, IProgress
    {
        private int _percentage;
        private string _progressText;
        private bool _cancelled = false;
        private object _tag;
        private bool _formLoaded = false;
        private bool _closeForm = false;

        public new object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        public bool Cancelled
        {
            get { return _cancelled; }
        }

        public ProgressForm()
        {
            InitializeComponent();

            label1.Text = "Busy.....";
        }

        private void UpdateStatus(Label _progressStatus, string statusMessage)
        {
            if (_progressStatus.InvokeRequired)
            {
                _progressStatus.Invoke(new Action(() => _progressStatus.Text = statusMessage));
            }
            else
            {
                if (_progressStatus.IsHandleCreated)
                {
                    _progressStatus.Text = statusMessage;
                }
            }
        }

        private void UpdateProgress(ProgressBar progressBar, int percentage)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.Invoke(new Action(() => progressBar.Value = progressBar.Maximum * percentage / 100));
            }
            else
            {
                if (progressBar.IsHandleCreated)
                {
                    progressBar.Value = progressBar.Maximum * percentage / 100;
                }
            }
        }

        public string ProgressText
        {
            get
            {
                return _progressText;
            }
            set
            {
                _progressText = value;
                UpdateStatus(label1, _progressText);
            }
        }

        public int Percentage
        {
            get
            {
                return _percentage;
            }
            set
            {
                if (value > 100)
                {
                    value = 100;
                }
                else if (value < 0)
                {
                    value = 0;
                }
                _percentage = value;
                UpdateProgress(_progressBar, _percentage);
            }
        }

        public void Finished()
        {
            Finished(0);
        }

        public void Finished(int count=0)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;

            if (_progressBar.InvokeRequired)
            {
                if (count > 100)
                {
                    Close();
                }
                else
                {
                    _progressBar.Invoke(new Action(() => Finished(count++)));
                }
            }
            else
            {
                if (_formLoaded)
                {
                    Close();
                }
                else
                {
                    _closeForm = true;
                }
            }

            // Close();
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            _cancelled = true;
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void ProgressForm_Load(object sender, EventArgs e)
        {
            _formLoaded = true;

            if (_closeForm)
            {
                Close();
            }
        }
    }
}
